package com.bingfor.easycablelink.data;


import com.bingfor.easycablelink.App;
import com.bingfor.easycablelink.BuildConfig;
import com.bingfor.easycablelink.utils.DeviceUtils;

import java.io.File;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by mod on 16-3-15.
 */
public class ApiRetrofit {
    public static String HOST = "http://kitchenpie.bingfor.com";
//    public static String HOST = "http://182.92.161.212/kitchenpie-php/public";
    public static final String API_HOST = HOST + "/index.php/api/";
    private static NetworkApi networkApi;
    public static OkHttpClient httpClient;
    protected static final Object monitor = new Object();

    private static Retrofit retrofit;

    private static CookieHandler cookieHandler;
    private static Cache cache;

    private ApiRetrofit() {
    }

    static {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        File cacheFile = App.getmContext().getExternalCacheDir();
        cache = new Cache(cacheFile, 10 * 1024 * 1024);
        //cache
//        Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = chain -> {
//
//            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
//            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
//            cacheBuilder.maxStale(365, TimeUnit.DAYS);
//            CacheControl cacheControl = cacheBuilder.build();
//
//            Request request = chain.request();
//            if (!DeviceUtils.hasInternet()) {
//                request = request.newBuilder()
//                        .cacheControl(cacheControl)
//                        .build();
//
//            }
//            Response originalResponse = chain.proceed(request);
//            if (DeviceUtils.hasInternet()) {
//                int maxAge = 0; // read from cache
//                return originalResponse.newBuilder()
//                        .removeHeader("Pragma")
//                        .header("Cache-Control", "public ,max-age=" + maxAge)
//                        .build();
//            } else {
//                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
//                return originalResponse.newBuilder()
//                        .removeHeader("Pragma")
//                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//                        .build();
//            }
//        };

        cookieHandler = new CookieManager();
        OkHttpClient.Builder builder = new OkHttpClient
                .Builder()
                .cache(cache)
                .connectTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
//                .addInterceptor(new CacheInterceptor())'
//                .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
//                .addNetworkInterceptor(new StethoInterceptor())
                .cookieJar(new JavaNetCookieJar(cookieHandler));
        if (BuildConfig.DEBUG)
            builder.addInterceptor(logging);
        httpClient = builder.build();
        retrofit = new Retrofit
                .Builder()
                .baseUrl(API_HOST)
//                .addConverterFactory(FastJsonConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClient)
                .build();
    }

    public static NetworkApi getInstance() {
        synchronized (monitor) {
            if (networkApi == null) {
                networkApi = retrofit.create(NetworkApi.class);
            }
            return networkApi;
        }
    }



}
